/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.gui.frames;

import java.io.File;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import sshtunneler.packager.data.AdditionalFiles;
import sshtunneler.packager.data.ScriptVariables;

public class ShowFiles extends MyInternalFrame {

	private static final long serialVersionUID = 4953418245046320932L;
	private static final String REMOVE_CHOICE = "<<Remove>>";
	private JComboBox table_cell_combo_box = new JComboBox();

	public ShowFiles(MyInternalFrameActionDispatcherFrame dispatcher) {
		super(dispatcher);
		this.initComponents();
		this.jTable1.getColumnModel().getColumn(1).setCellEditor(
				new DefaultCellEditor(this.table_cell_combo_box));
		dispatcher.add_additional_files_listener(this);
		dispatcher.add_script_variable_listener(this);
		this.refresh();
	}

	private void initComponents() {

		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.jTable1 = new javax.swing.JTable();
		this.jPanel1 = new javax.swing.JPanel();
		this.txt_newvarname = new MyJTextField();
		this.cmd_browse = new javax.swing.JButton();
		this.jComboBox1 = new javax.swing.JComboBox();
		this.cmd_add = new javax.swing.JButton();

		this.setClosable(true);
		this.setIconifiable(true);
		this.setResizable(true);
		this.setTitle("Additionnal files");
		this.setName("Form");

		this.jScrollPane1.setName("jScrollPane1");

		this.jTable1.setModel(new MyTableModel());
		this.jTable1.setName("jTable1");
		this.jScrollPane1.setViewportView(this.jTable1);

		this.getContentPane().add(this.jScrollPane1,
				java.awt.BorderLayout.CENTER);

		this.jPanel1.setName("jPanel1");

		this.txt_newvarname.setEditable(false);
		this.txt_newvarname.setMinimumSize(new java.awt.Dimension(80, 20));
		this.txt_newvarname.setName("txt_newvarname");
		this.txt_newvarname.setPreferredSize(new java.awt.Dimension(130, 20));
		this.jPanel1.add(this.txt_newvarname);

		this.cmd_browse.setText("...");
		this.cmd_browse.setName("cmd_browse");
		this.cmd_browse.setPreferredSize(new java.awt.Dimension(30, 23));
		this.cmd_browse.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ShowFiles.this.cmd_browseActionPerformed(evt);
			}
		});
		this.jPanel1.add(this.cmd_browse);

		this.jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
		this.jComboBox1.setName("jComboBox1");
		this.jComboBox1.setPreferredSize(new java.awt.Dimension(150, 20));
		this.jPanel1.add(this.jComboBox1);

		this.cmd_add.setText("add");
		this.cmd_add.setName("cmd_add");
		this.cmd_add.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ShowFiles.this.cmd_addActionPerformed(evt);
			}
		});
		this.jPanel1.add(this.cmd_add);

		this.getContentPane().add(this.jPanel1, java.awt.BorderLayout.PAGE_END);

		this.pack();
	}

	private void cmd_browseActionPerformed(java.awt.event.ActionEvent evt) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		chooser.setCurrentDirectory(new File("."));

		chooser.showOpenDialog(null);

		File selection = chooser.getSelectedFile();
		if (selection == null || !selection.exists()) {
			JOptionPane.showMessageDialog(this, "Illegal file.");
			return;
		}

		((MyJTextField) this.txt_newvarname).setInnerObject(selection);
	}

	private void cmd_addActionPerformed(java.awt.event.ActionEvent evt) {
		File local_file = ((MyJTextField) this.txt_newvarname).getInnerObject();
		String target = (String) this.jComboBox1.getSelectedItem();

		if (AdditionalFiles.inst.has_file(local_file)) {
			JOptionPane.showMessageDialog(this, "This file is already added.");
		} else {
			AdditionalFiles.inst.addFile(local_file, target);
			this.dispatcher.fire_additional_files_changed();
		}
	}

	public void refresh() {
		this.table_cell_combo_box.setModel(new DefaultComboBoxModel());
		this.jComboBox1.setModel(new DefaultComboBoxModel());
		this.table_cell_combo_box.addItem(REMOVE_CHOICE);
		for (String varname : ScriptVariables.inst.allVariables()) {
			this.table_cell_combo_box.addItem(varname);
			this.jComboBox1.addItem(varname);
		}

		((AbstractTableModel) this.jTable1.getModel()).fireTableDataChanged();
	}

	private javax.swing.JButton cmd_add;
	private javax.swing.JButton cmd_browse;
	private javax.swing.JComboBox jComboBox1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField txt_newvarname;

	class MyTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 2779732289079274993L;
		private String[] columnNames = { "Local", "Destination" };

		public int getColumnCount() {
			return this.columnNames.length;
		}

		public int getRowCount() {
			return AdditionalFiles.inst.files().size();
		}

		@Override
		public String getColumnName(int col) {
			return this.columnNames[col];
		}

		public File variableAt(int row) {
			return AdditionalFiles.inst.files().get(row);
		}

		public Object getValueAt(int row, int col) {
			if (col == 0) {
				return this.variableAt(row);
			} else {
				return AdditionalFiles.inst.destination_for(this
						.variableAt(row));
			}
		}

		@Override
		public Class<?> getColumnClass(int c) {
			return String.class;
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			// Note that the data/cell address is constant,
			// no matter where the cell appears onscreen.
			if (col == 0) {
				return false;
			} else {
				return true;
			}
		}

		@Override
		public void setValueAt(Object value, int row, int col) {
			String val = (String) value;
			if (val.equals(REMOVE_CHOICE)) {
				AdditionalFiles.inst.removeFile(this.variableAt(row));
			} else {
				AdditionalFiles.inst.addFile(this.variableAt(row), val);
			}
			ShowFiles.this.dispatcher.fire_additional_files_changed();
		}
	}

	class MyJTextField extends JTextField {

		private static final long serialVersionUID = 6036266842409033494L;
		File obj = null;

		public void setInnerObject(File obj) {
			this.obj = obj;
			this.setText(obj.getAbsolutePath());
		}

		public File getInnerObject() {
			return this.obj;
		}
	}
}
